package com.leo.manage_system.controller;


import com.leo.manage_system.entity.Advertise;
import com.leo.manage_system.entity.Announcement;
import com.leo.manage_system.service.AdvertiseService;
import com.leo.manage_system.service.AnnouncementService;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 公告 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@Api(tags = "公告管理")
@RestController
@RequestMapping("/service_manage/announcement")
@CrossOrigin
public class AnnouncementController {
    final AnnouncementService announcementService;
    JwtUtils jwtUtils = new JwtUtils();
    @Autowired
    public AnnouncementController(AnnouncementService announcementService){
        this.announcementService=announcementService;
    }

    @ApiOperation(value = "添加")
    @PostMapping("addAnnouncement")
    public R addAdmin(@ApiParam(name = "token", value = "token", required = true)
                      @RequestParam(value="token") String token,
                      @ApiParam(name = "公告",required = true) @RequestBody Announcement announcement)
    {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = announcementService.addAnnouncement(announcement);
            if (!code) {
                return R.error().message("添加失败");
            }
            return R.ok().message("添加成功");
        }
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("deleteAdvertise")
    public R deleteAdmin(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "id", value = "ID", required = true)
            @RequestParam String id){

        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = announcementService.deleteAnnouncement(id);
            if(!code) {
                return R.error().message("删除失败");
            }
            return R.ok().message("删除成功");
        }
    }
    @ApiOperation("编辑")
    @PostMapping("/modifyAdvertise")
    public R modifyRes(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam("广告") @RequestBody Announcement announcement) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = announcementService.updateById(announcement);
            if(!code) {
                return R.error().message("编辑失败");
            }
            return R.ok().message("编辑成功");
        }
    }
}

